/*
 * Copyright 2011 OpenWAF.com
 *
 * Licensed under the Apache License, Version 2.0 (the "License"); you may not
 * use this file except in compliance with the License. You may obtain a copy of
 * the License at
 *
 * http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
 * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
 * License for the specific language governing permissions and limitations under
 * the License.
 */

package com.openwaf.client.ui.widget;

import com.openwaf.client.core.Timer;
import com.openwaf.client.core.Window;
import com.openwaf.client.dom.Style;
import com.openwaf.client.event.HandlerRegistration;
import com.openwaf.client.event.dom.ClickEvent;
import com.openwaf.client.event.dom.ClickHandler;
import com.openwaf.client.ui.UIControl;


public class ColorPicker extends UIControl{
     public static interface ColorSelectHandler{
        void onColorSelect(String value);
    }
    private ColorPalette palette;
    private ClickHandler clickHandler;
    private HandlerRegistration clickReg;
    private boolean paletteShown;
    private ColorSelectHandler selectHandler;
    public ColorPicker(){
        paletteShown=false;
        getElement().addClickHandler(new ClickHandler() {
            public void onClick(ClickEvent event) {
                if(!paletteShown){
                    (new Timer() {
                        @Override
                        public void run() {
                            showPalette();
                        }
                    }).schedule(10);                    
                }
            }
        });        
    }
    public void setColorSelectHandler(ColorSelectHandler handler){
        selectHandler=handler;
    }
    public void showPalette(){
        if(palette==null){
            palette=new ColorPalette();
            Style s=palette.getElement().getStyle();
            s.setPosition("absolute");
            s.setDisplay("none");
            s.setZIndex(100);
            paletteShown=false;
            //int x=getElement().getClientHeight();
            //x=x-Integer.parseInt(getElement().getStyle().getPaddingTop());
            s.setMarginTop(""+0+"px");
            palette.setColorSelectHandler(new ColorSelectHandler() {
                public void onColorSelect(String value) {                    
                    getElement().getStyle().setBackgroundColor(value);
                    if(selectHandler!=null){
                        selectHandler.onColorSelect(value);
                    }
                }
            });
            getElement().appendChild(palette.getElement());
        }
        if(!paletteShown){
            palette.getElement().getStyle().setDisplay("");
            paletteShown=true;
            clickReg=Window.getDocument().addClickHandler(new ClickHandler() {
                public void onClick(ClickEvent event) {
                    hidePalette();
                }
            });
        }
    }
    public void hidePalette(){        
        if(paletteShown){
            clickReg.removeHandler();
            clickReg=null;
            palette.getElement().getStyle().setDisplay("none");
            paletteShown=false;
        }
    }

}
